package com.neshai.webapp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neshai.webapp.entity.AccountBill;
import com.neshai.webapp.entity.User;
import com.neshai.webapp.mapper.AccountBillMapper;
import com.neshai.webapp.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UpgradeService {

    private final UserMapper userMapper;
    private final AccountBillMapper accountBillMapper;

    //付款，并升级账户
    public boolean paidAndUpgradeByUserId(int userId, Long money) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            if (user.getVipLevel() == 0) {
                LocalDateTime now = LocalDateTime.now();
                updateWrapper.eq("user_id", user.getUserId()); // 设置更新条件
                updateWrapper.set("expiry_time", now.plusDays(31)); // 设置要更新的字段
                updateWrapper.set("recharge_time", now); // 设置要更新的字段
                updateWrapper.set("vip_level", 1);
                accountBillInsert(userId);
            } else {
                updateWrapper.eq("user_id", user.getUserId());
                updateWrapper.set("balance", user.getBalance() + money);
            }
            userMapper.update(null, updateWrapper);
            return true;
        } else {
            return false;
        }
    }

    public boolean paidAndUpgradeByEmail(String email, Long money) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        List<User> users = userMapper.selectList(queryWrapper);
        if (!users.isEmpty()) {
            User user = users.getFirst();
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            if (user.getVipLevel() == 0) {
                LocalDateTime now = LocalDateTime.now();
                updateWrapper.eq("user_id", user.getUserId()); // 设置更新条件
                updateWrapper.set("expiry_time", now.plusDays(31)); // 设置要更新的字段
                updateWrapper.set("recharge_time", now); // 设置要更新的字段
                updateWrapper.set("vip_level", 1);
                updateWrapper.set("subscription_status",1);

                accountBillInsert(user.getUserId());
            } else {
                updateWrapper.set("balance", user.getBalance() + money);
            }
            userMapper.update(null, updateWrapper);
            return true;
        } else {
            return false;
        }
    }

    //升级账户
    public boolean upgrade(int userId) {
        User user = userMapper.selectById(userId);
        if (user != null && user.getVipLevel() == 0 && user.getBalance() >= 2000) {
            user.setVipLevel(1);
            user.setBalance(user.getBalance() - 2000);
            userMapper.updateById(user);

            accountBillInsert(userId);
            return true;
        } else {
            return false;
        }
    }

    public void accountBillInsert(int userId) {
        AccountBill accountBill = new AccountBill();
        accountBill.setUserId(userId);
        accountBill.setPaidType(1);
        accountBill.setCreatedAt(LocalDateTime.now());
        accountBill.setAmountPaid(2000);
        accountBillMapper.insert(accountBill);
    }
}
